<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Eager_for_care extends CI_Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->load->model("customer_health_model", "health_model");
	}
	
	public function index()
	{
		$this->load->model('personal_health_info_model');
		
		$health_info = get_recent_health_info(get_uid());
		$health_info_fields = get_health_info_fields();
		
		$symp = $this->personal_health_info_model->get_symptoms_share(get_uid());		
		
		$data = array(
				"health_info"=>$health_info,
				"health_info_fields"=>$health_info_fields,
				'symptoms'=>$symp
				);
		
		$this->load->view("functionEntry/eagerForCare/eagerForCare", $data);
	}
	
	public function update_share_status()
	{
		$field = $this->input->post("field");
		$data = $this->input->post("data");

		$this->write_share_status($field, $data);
		
		if(!$this->health_model->should_push_health_info(get_uid(), $field, $data))
			return;

		$uid = get_uid();
		$health_info_id = $this->health_model->get_last_health_info_id($uid);
		$this->health_model->push_health_info($uid, $field, $health_info_id);
			


	}
	
	private function write_share_status($field, $data)
	{
		//data提前转换为bool值，后面可以不用纠结；
		$data = $data == "true" ? true : false;
	
		$query = $this->db->get_where("usershare", array(
				"uid"=>get_uid()
		));
	
		if($query->num_rows()){
			$record[$field] = $data;
			$this->db->where("uid", get_uid());
			$this->db->update("usershare", $record);
		}
		else {
			$record["uid"] = get_uid();
			$record[$field] = $data;
			$this->db->set($record);
			$this->db->insert("usershare");
		}
	
	}
}
?>